package com.comtime.smartbox;

import java.io.InputStream;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;

import com.comtime.login.LoginActivity;
import com.comtime.smartkilimanjaro.R;

public class GuideActivity extends BaseActivity {
	private View mView;
	private LayoutInflater mInflater;
	private ViewPager mViewPager;
	View view;

	boolean isCN=true;
	
	private int flag = 2;//1:welcome 2:about

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		flag = getIntent().getIntExtra("FLAG", 1);
		// ���ô����ޱ���
		// requestWindowFeature(Window.FEATURE_NO_TITLE);
		mInflater = getLayoutInflater();
		isCN=getResources().getConfiguration().locale.getCountry().equals("CN");
		mView = mInflater.inflate(R.layout.activity_guide2, null);
		mViewPager = (ViewPager) mView.findViewById(R.id.myviewpager);
		setContentView(mView);

		mViewPager.setAdapter(new MyPagerAdapter());
		mViewPager.setOnPageChangeListener(new OnPageChangeListener() {
			public void onPageSelected(int arg0) {
				
			}

			@Override
			public void onPageScrollStateChanged(int arg0) {

			}

			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {

			}
		});
	}
	@Override
	protected void onResume() {
		super.onResume();
	}

	@Override
	protected void onPause() {
		super.onPause();
	}
	ImageView guide_image_area, start;
	Bitmap bmp;

	class MyPagerAdapter extends PagerAdapter {

		@Override
		public int getCount() {
			return 4;
		}

		@Override
		public boolean isViewFromObject(View arg0, Object arg1) {
			return arg0 == arg1;
		}

		@Override
		public int getItemPosition(Object object) {
			return super.getItemPosition(object);
		}

		@Override
		public void destroyItem(View arg0, int arg1, Object arg2) {
			
			((ViewPager) arg0).removeView((View) arg2);
			//recycle(bmp);
		}

		@Override
		public Object instantiateItem(View arg0, int arg1) {// �ؼ�ط�����̬���ҳ�档
			view = mInflater.inflate(R.layout.guide_one, null);
			guide_image_area = (ImageView) view
					.findViewById(R.id.guide_image_area);
			
			if(bmp!=null&& bmp.isRecycled()){
				bmp.prepareToDraw();
			}
			switch (arg1) {
			case 0:
				bmp = readBitMap(GuideActivity.this, R.drawable.ic_launcher);
				guide_image_area.setImageBitmap(bmp);
				break;
			case 1:
				bmp = readBitMap(GuideActivity.this, R.drawable.ic_launcher);
				guide_image_area.setImageBitmap(bmp);
				break;
			case 2:
				bmp = readBitMap(GuideActivity.this, R.drawable.ic_launcher);
				guide_image_area.setImageBitmap(bmp);
				
				break;
			
			case 3:
				
				bmp = readBitMap(GuideActivity.this, R.drawable.ic_launcher);
				guide_image_area.setImageBitmap(bmp);
				guide_image_area.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View v) {
						
						Intent intent = null;
						
						
						if (flag == 1) {
							intent = new Intent(GuideActivity.this,
									LoginActivity.class);
							startActivity(intent);
							GuideActivity.this.finish();
						} else if (flag == 2) {
						
							GuideActivity.this.finish();
						}
						recycle(bmp);
					}

				});
				break;
				
			default:
				break;
			}
			view.setId(arg1);
			((ViewPager) arg0).addView(view);
			return view;
		}
	}

	
	@Override
	protected void onDestroy() {

		super.onDestroy();
	}

	/**
	 * 
	 * 
	 * @param context
	 * @param resId
	 * @return
	 */
	public static Bitmap readBitMap(Context context, int resId) {
		BitmapFactory.Options opt = new BitmapFactory.Options();
		opt.inPreferredConfig = Bitmap.Config.RGB_565;
		opt.inPurgeable = true;
		opt.inInputShareable = true;
		
		InputStream is = context.getResources().openRawResource(resId);
		return BitmapFactory.decodeStream(is, null, opt);
	}

	private void recycle(Bitmap bmp) {
		if (bmp!=null && !bmp.isRecycled()) {
			bmp.recycle(); // 
			System.gc(); // 
		}
	}
}
